//
//  GJFHomeViewController.m
//  Wedding
//
//  Created by 郭静芬 on 2016/12/20.
//  Copyright © 2016年 郭静芬. All rights reserved.
//

#import "GJFHomeViewController.h"
#import "GJFCarouselFigurView.h"
#import "GJFBannerModel.h"
#import "GJFWebViewController.h"
#import "GJFPlotModel.h"
#import "GJFClassifyTableViewCell.h"
#import "GJFPlotTableViewCell.h"
#import "GJFPlotDetailViewController.h"
#import "GJFClassifyViewController.h"
#import "GJFIntro.h"
#import "AppDelegate.h"

@interface GJFHomeViewController () <CarouselFigurViewDelegate, UITableViewDelegate, UITableViewDataSource, GJFClassifyTableViewCellDelegate>
@property (nonatomic, strong)GJFCarouselFigurView *carouseFigure;
@property (nonatomic, strong)NSMutableArray <GJFBannerModel *>*bannerArray;
@property (nonatomic, strong)UITableView *tableView;
@property (nonatomic, strong)NSMutableArray *plotArray;
@property (nonatomic, strong)GJFIntro *intro;
@end

@implementation GJFHomeViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = YES;
}
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    self.navigationController.navigationBar.hidden = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tableView.tableHeaderView = self.carouseFigure;
    [self getCarouseFigureData];
    [self getPlotData];
    if (![[NSUserDefaults standardUserDefaults] boolForKey:@"introFirst"]) {
        [self intro];// 显示引导页
    }
    else {
        [[UIApplication sharedApplication] setStatusBarHidden:NO];
    }
}
#pragma mark - 轮播图的网络请求
- (void)getCarouseFigureData {
    [self.view showBusyHUD];
    [GJFNetworkTool POSTRequestWithUrl:APP_CAROUSE params:@{} complete:^(NSDictionary *result) {
        [self.view hideBusyHUD];
        [self.tableView.mj_header endRefreshing];
        if ([result[@"code"] intValue] == 1000) {
            NSArray *array = result[@"data"];
            self.bannerArray = [GJFBannerModel transformWithArr:array];
            [self.carouseFigure refreshScrollView:self.bannerArray];
        }
    }];
}
#pragma mark - 轮播图点击触发协议方法
- (void)clickCarouselFigurViewWithIndex:(NSInteger)index {
    NSLog(@"%ld", index);
    GJFBannerModel *bannerModel = self.bannerArray[index];
    GJFWebViewController *web = [[GJFWebViewController alloc] init];
    [web createInfoWithTitle:bannerModel.title url:bannerModel.target_url options:nil];
    web.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:web animated:YES];
}
#pragma mark - 婚礼策划列表的网络请求
- (void)getPlotData {
    NSDictionary *dic = @{@"item": @"策划", @"page_index":@1};
    [GJFNetworkTool POSTRequestWithUrl:APP_PLOT params:dic complete:^(NSDictionary *result) {
        if ([result[@"code"] intValue] == 1000) {
            NSArray *array = result[@"data"];
            self.plotArray = [GJFPlotModel transformWithArr:array];
            [self.tableView reloadData];
        }
    }];
}
#pragma mark - TableView的分区
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}
#pragma mark - TableView每个分区的row数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 1;
    }else{
        return self.plotArray.count;
    }
}
#pragma mark - TableView分区中每个row的内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        GJFClassifyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ItemCell"];
        cell.delegate = self;
        return cell;
    }
    else {
        GJFPlotTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"GJFPlotTableViewCell"];
        cell.plotModel = self.plotArray[indexPath.row];
        return cell;
    }
}
#pragma mark - TableView每个分区中row的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return 190 * YY;
    }
    else {
        return 275 * YY;
    }
}
#pragma mark - TableView每个分区头部的高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 10 * YY;
}
#pragma mark - TableView每个分区尾部的高度
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.1;
}
#pragma mark - TableView的跳转方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    GJFPlotDetailViewController *plotDetailVC = [[GJFPlotDetailViewController alloc] init];
    plotDetailVC.plotId = [self.plotArray[indexPath.row] id];
    plotDetailVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:plotDetailVC animated:YES];
}
#pragma mark - 8个分类的点击方法
- (void)clickItemWithClassifyTitle:(NSString *)classifyTitle {
    GJFClassifyViewController *vc = [[GJFClassifyViewController alloc] init];
    vc.classifyTitle = classifyTitle;
    vc.hidesBottomBarWhenPushed = YES;
    vc.navigationItem.title = classifyTitle;
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - 懒加载
- (GJFCarouselFigurView *)carouseFigure {
    if (!_carouseFigure) {
        _carouseFigure = [[GJFCarouselFigurView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 200 * YY)];
        _carouseFigure.delegate = self;
    }
    return _carouseFigure;
}
- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 49) style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.mj_header = [GJFRefreshHeader headerWithRefreshingBlock:^{
            [self getPlotData];
            [self getCarouseFigureData];
        }];
        [_tableView registerClass:[GJFClassifyTableViewCell class] forCellReuseIdentifier:@"ItemCell"];
        [_tableView registerClass:[GJFPlotTableViewCell class] forCellReuseIdentifier:@"GJFPlotTableViewCell"];
        [self.view addSubview:_tableView];
    }
    return _tableView;
}
- (GJFIntro *)intro {
    if (!_intro) {
        _intro = [[GJFIntro alloc] initWithFrame:self.view.bounds];
        UIWindow *window = [(AppDelegate *)[UIApplication sharedApplication].delegate window];
        [window addSubview:_intro];
    }
    return _intro;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
